package com.studerb.view.headers;

import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.studerb.model.Header;
import com.studerb.model.HeaderThread;
import com.studerb.model.Post;
import com.studerb.service.interfaces.IHeaderService;
import com.studerb.util.Util;

public class PostPropertiesDlg extends JDialog {
	@Autowired IHeaderService headerService;
	protected Post post;

	protected JPanel propertiesPanel;
	protected JScrollPane scrollPane;

	// Post Componenets
	protected JLabel subjectLabel;
	protected JLabel datePostedLabel;
	protected JLabel byteCountLabel;
	protected JLabel authorLabel;
	protected JLabel newsGroupLabel;
	protected JLabel articleNumberLabel;
	protected JLabel articleDownloadedLabel;
	protected JLabel fileNameLabel;
	protected JTextField subjectField;
	protected JTextField datePostedField;
	protected JTextField byteCountField;
	protected JTextField authorField;
	protected JTextField newsGroupField;
	protected JTextField articleNumberField;
	protected JTextField articleDownloadedField;
	protected JTextField fileNameField;

	// HeaderThread Componenets
	protected JLabel zeroIndexedLabel;
	protected JLabel completeLabel;
	protected JLabel maxPartLabel;
	protected JTextField zeroIndexedField;
	protected JTextField completeField;
	protected JTextField maxPartField;

	// HeaderComponents
	protected JLabel messageIdLabel;
	protected JLabel currentPartLabel;
	protected JTextField messageIdField;
	protected JTextField currentPartField;

	public PostPropertiesDlg(JFrame frame, Post post) {
		super(frame, true);
		this.post = post;
	}

	// add header info if this is multipart
	private void addHeaders(HeaderThread headerThread) {
		List<Header> headers = headerService.getHeadersByThread((HeaderThread) post);

		String[] columnNames = new String[] { "Part #", "Article #", "MessageId" };
		Object[][] rows = new Object[headers.size()][];
		for (int x = 0; x < headers.size(); ++x) {
			Header header = headers.get(x);
			Object[] values = new Object[columnNames.length];
			for (int i = 0; i < columnNames.length; ++i) {
				switch (i) {
				case 0:
					values[i] = header.getCurrentPart();
					break;
				case 1:
					values[i] = header.getArticleNumber();
					break;
				case 2:
					values[i] = header.getMessageId();
					break;
				}
			}
			rows[x] = values;
		}

		JTable headersTable = new JTable(rows, columnNames);
		JScrollPane scrollPane = new JScrollPane(headersTable);
		headersTable.setFillsViewportHeight(true);
		propertiesPanel.add(scrollPane, "span 6, growx, wrap para");
	}

	public void init() {
		initComponents();
		JButton closeButton = new JButton("Close");
		propertiesPanel.add(closeButton, "wmin button, span 6, align right");
		closeButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				dispose();
			}
		});

		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		pack();
	}

	private void initComponents() {
		propertiesPanel = new JPanel(new MigLayout(new LC().insets("20")));

		if (post instanceof Header) {
			initHeaderComponents();
		}
		else if (post instanceof HeaderThread) {
			initHeaderThreadComponents();
		}

		scrollPane = new JScrollPane(propertiesPanel);
		this.add(scrollPane);
	}

	private void initHeaderComponents() {
		Header header = (Header) post;
		initPostComponents();

		currentPartLabel = new JLabel("Part#");
		propertiesPanel.add(currentPartLabel);
		currentPartField = new JTextField(String.valueOf(header.getCurrentPart()));
		propertiesPanel.add(currentPartField);
		currentPartField.setEditable(false);

		messageIdLabel = new JLabel("Message Id");
		propertiesPanel.add(messageIdLabel, "align right");
		messageIdField = new JTextField(header.getMessageId(), 40);
		messageIdField.setEditable(false);
		propertiesPanel.add(messageIdField, "span 4, wrap para");

	}

	private void initHeaderThreadComponents() {
		initPostComponents();
		HeaderThread headerThread = (HeaderThread) post;

		zeroIndexedLabel = new JLabel("ZeroPart");
		propertiesPanel.add(zeroIndexedLabel);
		zeroIndexedField = new JTextField(String.valueOf(headerThread.isZeroIndexed()), 10);
		propertiesPanel.add(zeroIndexedField);

		completeLabel = new JLabel("Complete");
		propertiesPanel.add(completeLabel, "align right");
		completeField = new JTextField(String.valueOf(headerThread.isComplete()), 10);
		propertiesPanel.add(completeField);

		maxPartLabel = new JLabel("Max Part");
		propertiesPanel.add(maxPartLabel, "align right");
		maxPartField = new JTextField(String.valueOf(headerThread.getMaxPart()), 10);
		propertiesPanel.add(maxPartField, "wrap para");

		zeroIndexedField.setEditable(false);
		completeField.setEditable(false);
		maxPartField.setEditable(false);

		addHeaders(headerThread);
	}

	private void initPostComponents() {
		// row 1
		subjectLabel = new JLabel("Subject");
		propertiesPanel.add(subjectLabel);
		subjectField = new JTextField(post.getSubject(), 50);
		propertiesPanel.add(subjectField, "span 5, growx, wrap");

		// row 2
		fileNameLabel = new JLabel("FileName");
		propertiesPanel.add(fileNameLabel);
		fileNameField = new JTextField(/* post.getFileName() */"unknown", 50);
		propertiesPanel.add(fileNameField, "span 5, growx, wrap");

		// row 3
		datePostedLabel = new JLabel("Date Posted");
		propertiesPanel.add(datePostedLabel);
		datePostedField = new JTextField(Util.getSimpleDateTime(post.getDatePosted()), 25);
		propertiesPanel.add(datePostedField);

		byteCountLabel = new JLabel("Size");
		propertiesPanel.add(byteCountLabel, "align right");
		byteCountField = new JTextField(FileUtils.byteCountToDisplaySize(post.getByteCount()), 15);
		propertiesPanel.add(byteCountField);

		articleDownloadedLabel = new JLabel("Downloaded");
		propertiesPanel.add(articleDownloadedLabel, "align right");
		articleDownloadedField = new JTextField(String.valueOf(post.isArticleDownloaded()), 7);
		propertiesPanel.add(articleDownloadedField, "wrap");

		// row 4
		authorLabel = new JLabel("Author");
		propertiesPanel.add(authorLabel);
		authorField = new JTextField(post.getAuthor(), 30);
		propertiesPanel.add(authorField, "span 3");

		articleNumberLabel = new JLabel("Article Number");
		propertiesPanel.add(articleNumberLabel, "align right");
		articleNumberField = new JTextField(String.valueOf(post.getArticleNumber()), 15);
		propertiesPanel.add(articleNumberField, "wrap");

		// row 5
		newsGroupLabel = new JLabel("NewsGroup");
		propertiesPanel.add(newsGroupLabel);
		newsGroupField = new JTextField(post.getNewsGroup().getGroupName(), 25);
		propertiesPanel.add(newsGroupField, "span 5, wrap");

		subjectField.setEditable(false);
		datePostedField.setEditable(false);
		byteCountField.setEditable(false);
		authorField.setEditable(false);
		newsGroupField.setEditable(false);
		articleNumberField.setEditable(false);
		articleDownloadedField.setEditable(false);
		fileNameField.setEditable(false);
	}
}
